﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PasswdTool
{
    public class Generate
    {
        private readonly char[] lower = "abcdefgjigklmnopqrstuvwxyz".ToCharArray();
        private readonly char[] upper = "ABCDEFGJIGKLMNOPQRSTUVWXYZ".ToCharArray();
        private readonly char[] num = "!@#$%.,[]{}".ToCharArray();
        private readonly char[] symbol = "0123456789".ToCharArray();
        private readonly List<Char> pool = new List<char>();
        public Generate(bool hasLower,bool hasUpper,bool hasNum,bool hasSymbol)
        {
            List<Char> list = new List<char>();
            if (hasLower)
            {
                list.AddRange(lower);
            }
            if (hasUpper)
            {
                list.AddRange(upper);
            }
            if (hasNum)
            {
                list.AddRange(num);
            }
            if (hasSymbol)
            {
                list.AddRange(symbol);
            }
            // 将集合乱序
            var random = new Random();
            var newList = new List<Char>();
            foreach (var item in list)
            {
                pool.Insert(random.Next(newList.Count), item);
            }
        }
        public string GeneratePwd(int length)
        {
            StringBuilder sb = new StringBuilder();
            Random random = new Random();
            for (int i = length - 1; i >= 0; i--)
            {
                sb.Append(pool[random.Next(pool.Count)]);
            }
            return sb.ToString();
        }

        public static byte[] GenerateKey(int length)
        {
            byte[] key = new byte[length];
            Random random = new Random();
            random.NextBytes(key);
            return key;
        }

    }
}
